require.config({
  baseUrl: "../javascripts/library/",
  paths: {
    jquery: "jquery",
    axios: "axios",
    cookies: "cookies",
  },
});

require(["jquery", "axios", "cookies"], function ($, axios, cookies) {
  let cookie = cookies.get("shop");
  let arr = cookie.split("-");
  let temp = "";
  arr.pop();
  axios
    .get("/product/getItems")
    .then((res) => {
      let data = res.data;
      data.forEach((el) => {
        let pic = JSON.parse(el.picture);
        arr.forEach((elm) => {
          elm = JSON.parse(elm);
          if (elm.id == el.id) {
            temp += `
              <tr
                  class="itemsList cart-item"
                  data-id="${el.id}"
                  data-promotionid=""
                  data-num="1"
                >
                  <td class="check_goods">
                    <span
                      class="common-checkbox checked j_operate"
                      data-op="select"
                      data-val="0"
                    ></span>
                  </td>
                  <td class="goods_info">
                    <figure class="goods-info">
                      <a
                        href=""
                      >
                        <img
                          src="../${pic[0].src}"
                          alt=""
                        />
                      </a>
                      <figcaption>
                        <a
                          class="item-title"
                          href=""
                        >
                          ${el.title}
                        </a>
                        <!--更改促销-->

                        <!--更改促销选项-->
                        <div class="promtions-box"></div>
                      </figcaption>
                    </figure>
                  </td>
                  <td>
                    <span>分类:AR1C</span>
                  </td>
                  <td>
                    <p class="price"><span>¥${el.nowprice}.00</span></p>
                  </td>
                  <td class="goods_num">
                    <div
                      class="mod-modifier fr"
                      data-monitor="shopcart_number_click"
                    >
                      <a
                        href="javascript:;"
                        class="modifier-sub j_operate disabled"
                        data-id=""
                        data-op="add"
                        data-val="-1"
                        >-</a
                      >

                      <input
                        type="tel"
                        class="modifier-value"
                        id="goods-num"
                        data-id=""
                        value="${elm.num}"
                        data-count="1"
                        data-maxcount="200"
                      />

                      <a
                        href="javascript:;"
                        class="modifier-add j_operate"
                        data-op="add"
                        data-id=""
                        data-val="1"
                        >+</a
                      >
                    </div>
                  </td>
                  <td class="item_total">
                    <!-- 库存紧张 -->
                    <p class="total"><span>¥${
                      el.nowprice * elm.num
                    }.00</span></p>
                  </td>
                  <td>
                    <span class="js_del" data-val="" data-id="">删除</span>
                  </td>
                </tr>
              `;
          }
        });
      });
      $($(".mod-cart-list")[0]).append(temp);
      $.each($(".js_del"), (i, el) => {
        $(el).on("click", function (ev) {
          let id = $(ev.target.parentNode.parentNode).attr("data-id");
          for (let i in arr) {
            let elm = JSON.parse(arr[i]);
            if (id == elm.id) {
              arr.splice(i, 1);
            }
          }
          let temp = arr.join("-").concat("-");
          cookies.set("shop", temp);
          location.reload();
        });
      });
    })
    .catch((err) => {
      console.error(err);
    });
});
